package com.beijiteshop.shop.ui.shopcar.adapter

import android.view.View
import androidx.databinding.ViewDataBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.beijiteshop.shop.R
import com.beijiteshop.shop.databinding.ItemShopCarProductBinding
import com.beijiteshop.shop.model.entity.ShopCarProductEntity

/**
 * 购物车首页Adapter
 */
class ShopCarAdapter(data: MutableList<ShopCarProductEntity>) : BaseQuickAdapter<ShopCarProductEntity,BaseDataBindingHolder<ViewDataBinding>>(
    R.layout.item_shop_car_product,data){

    init {
        addChildClickViewIds(R.id.shop_car_select_iv,R.id.shop_car_reduce_tv,R.id.shop_car_plus_tv)
    }

    override fun convert(holder: BaseDataBindingHolder<ViewDataBinding>, item: ShopCarProductEntity) {
        val binding = holder.dataBinding as? ItemShopCarProductBinding
        binding?.let {
            it.shopCarSelectIv.isSelected = item.isSelect
            it.shopCarNumTv.text = item.num.get().toString()
            it.product = item
            //规格、颜色
            val specAndColor = StringBuilder()
            specAndColor.append(item.productBean.specification)
            if (item.productBean.color.isNotEmpty()){
                specAndColor.append("·")
                specAndColor.append(item.productBean.color)
            }
            it.shopCarDescTv.text = specAndColor

            if (item.productBean.sellOut){
                it.shopCarSaleStateTv.text = context.resources.getString(R.string.sale_out)
                it.shopCarSaleStateTv.visibility =  View.VISIBLE
                it.shopCarSelectIv.visibility = View.INVISIBLE
            }else{
                it.shopCarSaleStateTv.visibility =  View.GONE
                it.shopCarSelectIv.visibility = View.VISIBLE
            }
            if (item.productBean.undercarriage){
                it.shopCarSaleStateTv.text = context.resources.getString(R.string.undercarriage)
                it.shopCarSaleStateTv.visibility =  View.VISIBLE
                it.shopCarSelectIv.visibility = View.INVISIBLE
            }else{
                it.shopCarSaleStateTv.visibility =  View.GONE
                it.shopCarSelectIv.visibility = View.VISIBLE
            }
        }
    }
}